<?php

if (!defined('BASEPATH'))
exit('No direct script access allowed');

class Findanexpert extends MX_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 * 	- or -
	 * 		http://example.com/index.php/welcome/index
	 * 	- or -
	 * Since this controller is set as the default controller in
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function Findanexpert() {
		parent::__construct();
		$this->load->model('lookupmodel');
		$this->load->model('helpermodel');
		//check is logged in
		$this->load->model('languagemodel');
		$this->load->model('projects/mdl_projectsmodel');
		$this->load->model('findanexpert/mdl_findanexpert');
		$this->language = $this->languagemodel->is_language_using();
		$this->load->helper('MY_url_helper');
		$this->load->helper('text');
		$this->load->helper('utf8');
		$this->load->model('imagemodel');
		
		//right side banner
		$this->load->model('banners/bannermodel');
	}

	public function index() {
		$this->load->model('imagemodel');
		$this->lang->load('findanexpert/findanexpert', $this->language);
		$this->lang->load('common/header', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');
		$data['text_heading'] = $this->lang->line('text_heading');
		$data['text_find_agents'] = $this->lang->line('text_find_agents');
		$data['text_specialities'] = $this->lang->line('text_specialities');
		$data['text_price_min'] = $this->lang->line('text_price_min');
		$data['text_price_max'] = $this->lang->line('text_price_max');
		$data['text_best_match'] = $this->lang->line('text_best_match');
		$data['text_certifications'] = $this->lang->line('text_certifications');
		$data['text_languages'] = $this->lang->line('text_languages');
		$data['text_agentname_company'] = $this->lang->line('text_agentname_company');
		$data['text_broker_with'] = $this->lang->line('text_broker_with');
		$data['text_company_agent'] = $this->lang->line('text_company_agent');
		$data['text_cea_reg_num'] = $this->lang->line('text_cea_reg_num');
		$data['text_cea_com_num'] = $this->lang->line('text_cea_com_num');
		$data['text_contact_this_agent'] = $this->lang->line('text_contact_this_agent');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'findanexpert.html'
		);
		
		$data['specialities'] = $this->mdl_findanexpert->getSpecialities($this->config->item('config_language_id'));
		$data['pricelist'] = $this->mdl_findanexpert->getPriceList($this->config->item('config_language_id'));
		$data['certifications'] = $this->mdl_findanexpert->getCertifications($this->config->item('config_language_id'));
		$data['languages'] = $this->mdl_findanexpert->getLanguages($this->config->item('config_language_id'));		

		$data['action'] = base_url() . 'findanexpert.html?';
		
		$options['language'] = $this->config->item('config_language_id');

		$options['country'] = NULL;

		$cities = $this->input->get('cities');
		$data['cities'] = $cities;
		
		if($cities != "") {
			$options['cities'] = $this->input->get('cities');
		} else {
			$options['cities'] = NULL;
		}
		
		$locations = $this->input->get('locations');
		$data['locations'] = $locations;

		if ($locations != "") {
			$options['locations'] = $this->input->get('locations');
		} else {
			$options['locations'] = NULL;
		}

		$subarea = $this->input->get('chkSubArea');
		$data['subarea'] = $subarea;

		if ($subarea != "") {
			$options['subarea'] = $this->input->get('chkSubArea');
		} else {
			$options['subarea'] = NULL;
		}

		$options['list_type'] = $this->input->get('list_type');
		if($options['list_type'] != "") {
			$options['list_type'] = $this->input->get('list_type');
		} else {
			$options['list_type'] = NULL;
		}

		$options['propertyfor'] = $this->input->get('propertyfor');
		$data['propertyfor'] = $options['propertyfor'];
		if($options['propertyfor'] != "") {
			$options['propertyfor'] = $this->input->get('propertyfor');
		} else {
			$options['propertyfor'] = NULL;
		}

		$options['propertytypeclss'] = $this->input->get('ddlPropertyClss');
		$data['propertytypeclss'] = $options['propertytypeclss'];
		if($options['propertytypeclss'] != "") {
			$options['propertytypeclss'] = $this->input->get('ddlPropertyClss');
		} else {
			$options['propertytypeclss'] = NULL;
		}

		$options['propertytype'] = $this->input->get('ddlPropertyType');
		$data['propertytype'] = $options['propertytype'];
		if($options['propertytype'] != "") {
			$options['propertytype'] = $this->input->get('ddlPropertyType');
		} else {
			$options['propertytype'] = NULL;
		}
		
		$options['buyerorseller'] = $this->input->get('buyerorseller');
		if($options['buyerorseller'] != "") {
			$options['buyerorseller'] = $this->input->get('buyerorseller');
		} else {
			$options['buyerorseller'] = 'buyer';
		}
		
		//temporary reset it to seller
		$options['buyerorseller'] = NULL;
		
		$data['buyerorseller'] = $options['buyerorseller'];
		
		$options['agentname'] = $this->input->get('agentname');
		$data['agentname'] = $options['agentname'];
		if($options['agentname'] != "") {
			$options['agentname'] = $this->input->get('agentname');
		} else {
			$options['agentname'] = NULL;
		}
		

		$options['price_min'] = $this->input->get('price_min');
		$data['price_min'] = $options['price_min'];
		if($options['price_min'] != "") {
			$options['price_min'] = $this->input->get('price_min');
		} else {
			$options['price_min'] = NULL;
		}

		$options['price_max'] = $this->input->get('price_max');
		$data['price_max'] = $options['price_max'];
		if($options['price_max'] != "") {
			$options['price_max'] = $this->input->get('price_max');
		} else {
			$options['price_max'] = NULL;
		}

		$options['keyword'] = $this->input->get('keyword');
		$data['keyword'] = $options['keyword'];
		if($options['keyword'] != "") {
			$options['keyword'] = $this->input->get('keyword');
		} else {
			$options['keyword'] = NULL;
		}

		$options['listing_id'] = NULL;
		//Features
		$features = $this->input->get('feature_category');

		if ($this->helpermodel->check_is_array($features)) {
			$options['features'] = implode(',', $features);
		} else {
			$options['features'] = NULL;
		}

		//Facilities
		$facilities = $this->input->get('facilities_category');

		if ($this->helpermodel->check_is_array($facilities)) {
			$options['facilities'] = implode(',', $facilities);
		} else {
			$options['facilities'] = NULL;
		}

		if ($this->input->get('sort')) {
			$sort = $this->input->get('sort');
			$data['sort'] = $sort;

			if ($sort == 'project-asc') {
				$options['orderby'] = "ProjectName_". $this->config->item('config_language_id') ." ASC";
			}

			if ($sort == 'project-desc') {
				$options['orderby'] = "ProjectName_". $this->config->item('config_language_id') ." DESC";
			}
    
			if ($sort == 'propertytype-asc') {
				$options['orderby'] = "PropertyType_". $this->config->item('config_language_id') ." ASC";
			}

			if ($sort == 'propertytype-desc') {
				$options['orderby'] = "PropertyType_". $this->config->item('config_language_id') ." DESC";
			}

			if ($sort == 'city-asc') {
				$options['orderby'] = "City_". $this->config->item('config_language_id') ." ASC";
			}

			if ($sort == 'city-desc') {
				$options['orderby'] = "City_". $this->config->item('config_language_id') ." ASC";
			}
		} else {
			$options['orderby'] = "BusinessName ASC";
		}

		if ($this->input->get('page')) {
			$options['page'] = $this->input->get('page');
		}
		else{
			$options['page'] = 1;
		}

		if($this->input->get('limit')) {
			$limit = $this->input->get('limit');
		} else {
			$limit = PER_PAGE;
		}

		$data['limit'] = $limit;

		$options['offset'] = ($options['page'] - 1) * $limit;
		$options['limit'] = $limit + $options['offset'];
		$options['listing_id'] = NULL;

		$results = $this->mdl_findanexpert->getAgents($options);
		
		$data['agents'] = array();
		foreach ($results as $result) {
			//Agent Photo
			//$agentPhoto = $this->mdl_findanexpert->getAgentPhoto($result['User_ID']);
			$agentPhoto = "";
			//Recommendations
			$recommendations = $this->mdl_findanexpert->getRecommendations($result['User_ID']);
			
			//$mobile_ext = substr($result['Mobile'], 0, 2);
			//$mobile_num = substr($result['Mobile'], 2, strlen($result['Mobile']));
			//$mobile = "(" . $mobile_ext . ") " . $mobile_num;
			$mobile = MOBILE_EXT . $result['Mobile'];
			
			$data['agents'][] = array(
				'agent_id' 			=> $result['User_ID'],
				'busniessname'		=> $result['BusinessName'],
				'email'				=> $result['Email'],
				'mobile'			=> $mobile,
				'image'				=> $agentPhoto,
				'total_listing'		=> $this->mdl_findanexpert->getTotalProjectListing($result['User_ID']),
				'recommendations'	=> $recommendations,
				'LicenceNo'			=> $result['LicenceNo'],
				'AgentLicenceNo'	=> $result['CEA_LicenceNo']
			);
		}

		// paging
		if (isset($results[0])) {
			$data['total_count'] = $results[0]['total'];
		} else {
			$data['total_count'] = 0;
		}

		//
		$url = '';

		$url .= '&localorint=' . $this->input->get('localorint');

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			$url .= $cityStr;
		}
		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			$url .= $subareaStr;
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}

		if($this->input->get('limit')) {
			$url .= '&limit=' . $this->input->get('limit');
		}

		$data['sorts'] = array();

		$data['sorts'][] = array(
			'text'  => $this->lang->line('text_project_asc'),
			'value' => 'project-asc',
			'href'  => base_url() . 'projects.html?sort=project-asc' . $url
		);
			
		$data['sorts'][] = array(
			'text'  => $this->lang->line('text_project_desc'),
			'value' => 'project-desc',
			'href'  => base_url() . 'projects.html?sort=project-desc' . $url
		);

		$data['sorts'][] = array(
			'text'  => $this->lang->line('text_propertytype_asc'),
			'value' => 'propertytype-asc',
			'href'  => base_url() . 'projects.html?sort=propertytype-asc' . $url
		);
			
		$data['sorts'][] = array(
			'text'  => $this->lang->line('text_propertytype_desc'),
			'value' => 'propertytype-desc',
			'href'  => base_url() . 'projects.html?sort=propertytype-desc' . $url
		);

		$data['sorts'][] = array(
			'text'  => $this->lang->line('text_city_asc'),
			'value' => 'city-asc',
			'href'  => base_url() . 'findanexpert.html?sort=city-asc' . $url
		);

		$data['sorts'][] = array(
		'text'  => $this->lang->line('text_city_desc'),
		'value' => 'city-desc',
		'href'  => base_url() . 'findanexpert.html?sort=city-desc' . $url
		);

		//Scope for limiting
		$url = '';

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			$url .= $cityStr;
		}

		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			$url .= $subareaStr;
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}

		if ($this->input->get('sort')) {
			$url .= '&sort=' . $this->input->get('sort');
		}

		if ($this->input->get('page')) {
			$url .= '&page=' . $this->input->get('page');
		}

		$data['limits'] = array();

		$limits = array_unique(array(PER_PAGE, 20, 30, 40));

		sort($limits);

		foreach($limits as $limits){
			$data['limits'][] = array(
			'text'  => $limits,
			'value' => $limits,
			'href'  => base_url() . 'findanexpert.html?' . $url . '&limit=' . $limits
			);
		}

		//Scope for Paging
		$url = '';
		if ($this->input->get('sort')) {
			$url .= '&sort=' . $this->input->get('sort');
		}

		if($this->input->get('ddlPropertyType')) {
			$url .= '&ddlPropertyType=' . $this->input->get('ddlPropertyType');
		}

		$cityStr = "";
		if($this->input->get('cities')) {
			$cityArr = array();
			if(!is_array($this->input->get('cities'))) {
				$cityArr = explode(',', $this->input->get('cities'));
			} else {
				$cityArr = $this->input->get('cities');
			}

			foreach ($cityArr as $key => $value) {
				$cityStr .= '&cities[]=' . $value;
			}
			$url .= $cityStr;
		}

		$locationStr = "";
		if($this->input->get('locations')) {
			foreach ($this->input->get('locations') as $key => $value) {
				$locationStr .= '&locations[]=' . $value;
			}
			$url .= $locationStr;
		}

		$subareaStr = "";
		if($this->input->get('chkSubArea')) {
			foreach ($this->input->get('chkSubArea') as $key => $value) {
				$subareaStr .= '&chkSubArea[]=' . $value;
			}
			$url .= $subareaStr;
		}

		if($this->input->get('keyword')) {
			$url .= '&keyword=' . urlencode(html_entity_decode($this->input->get('keyword'), ENT_QUOTES, 'UTF-8'));;
		}
		 
		if ($this->input->get('sort')) {
			$url .= '&sort=' . $this->input->get('sort');
		}

		if ($this->input->get('limit')) {
			$url .= '&limit=' . $this->input->get('limit');
		}

		$this->load->library('pagination');
		$config['base_url'] = base_url() . "findanexpert.html?" . $url;
		$config['enable_query_strings'] = TRUE;
		$config['page_query_string'] = TRUE;
		$config['use_page_numbers'] = TRUE;
		$config['query_string_segment'] = 'page';
		$config['per_page'] = $limit;
		$config['total_rows'] = $data['total_count'];

		$this->pagination->initialize($config);
		$data['paginator'] = $this->pagination->create_links();
		
		//get banner for right side
		$data['banner'] = $this->bannermodel->getBanners('Result', $this->config->item('market'), $this->config->item('config_language_id'));

		$data['module'] = "findanexpert";
		$data['view_file'] = "findanexpert";

		echo Modules::run('template/default_template', $data);
	}
	
	function detail() {
		if($this->uri->segment(3)) {
			$User_ID = $this->uri->segment(3);
		} else {
			$User_ID = 0;
		}
		
		
		$this->load->model('imagemodel');
		$this->lang->load('findanexpert/findanexpert', $this->language);
		$this->lang->load('common/header', $this->language);

		//SEO
		$data['title'] = $this->lang->line('text_title');
		$data['description'] = $this->lang->line('text_description');
		$data['keywords'] = $this->lang->line('text_keywords');
		$data['text_heading'] = $this->lang->line('text_heading');
		$data['text_contact_this_agent'] = $this->lang->line('text_contact_this_agent');
		$data['text_broker_with'] = $this->lang->line('text_broker_with');
		$data['text_company_agent'] = $this->lang->line('text_company_agent');
		$data['text_cea_com_num'] = $this->lang->line('text_cea_com_num');
		$data['text_cea_reg_num'] = $this->lang->line('text_cea_reg_num');

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text'      => 'Home',
			'href'      => base_url()
		);

		$data['breadcrumbs'][] = array(
			'text'      => $this->lang->line('text_heading'),
			'href'      => base_url() . 'findanexpert.html'
		);
		
		$options['language'] = $this->config->item('config_language_id');
		$options['country'] = NULL;
		$options['cities'] = NULL;
		$options['locations'] = NULL;
		$options['subarea'] = NULL;
		$options['buyerorseller'] = NULL;
		$options['agentname'] = NULL;
		$options['propertytype'] = NULL;
		$options['offset'] = NULL;
		$options['limit'] = NULL;
		$options['orderby'] = NULL;
		$options['listing_id'] = $User_ID;
		
		$findanexpert_info = $this->mdl_findanexpert->getAgents($options);
		
		if(isset($findanexpert_info) && is_array($findanexpert_info) && count($findanexpert_info) > 0) {
			$data['findanexpert_info'] = $findanexpert_info[0];
			
			//$agent_mobile = $data['findanexpert_info']['Mobile'];
			//$mobile_ext = substr($agent_mobile, 0, 2);
			//$mobile_num = substr($agent_mobile, 2, strlen($agent_mobile));
			//$mobile = "(" . $mobile_ext . ") " . $mobile_num;
			$mobile = MOBILE_EXT . $data['findanexpert_info']['Mobile'];
			$data['Mobile'] = $mobile;
			
			$data['breadcrumbs'][] = array(
				'text'      => $data['findanexpert_info']['BusinessName'],
				'href'      => base_url() . 'find-an-expert/detail/' . $User_ID 
			);
			
			//get media for listing
			$data['agentPhoto'] = $this->mdl_findanexpert->getAgentPhoto($User_ID);
			
			//get Agent Property Type
			$data['agentPropertyType'] = $this->mdl_findanexpert->getAgentPropertyType($User_ID);
			$data['propertytype'] = array();
			$data['strPropertyType'] = "";
			
			if(is_array($data['agentPropertyType']) && count($data['agentPropertyType']) > 0) {
				foreach ($data['agentPropertyType'] as $propertytype) {
					array_push($data['propertytype'], $propertytype->Name);
				}
				
				$data['strPropertyType'] = implode(', ', $data['propertytype']);
			}
			
			//get Agent Speciality
			$data['agentSpeciality'] = $this->mdl_findanexpert->getAgentSpeciality($User_ID);
			$data['speciality'] = array();
			$data['strSpeciality'] = "";
			
			if(is_array($data['agentSpeciality']) && count($data['agentSpeciality']) > 0) {
				foreach ($data['agentSpeciality'] as $speciality) {
					array_push($data['speciality'], $speciality->SpecialtyName);
				}
				
				$data['strSpeciality'] = implode(', ', $data['speciality']);
			}
			
			//get Agent Language
			$data['agentLanguage'] = $this->mdl_findanexpert->getAgentLanguage($User_ID);
			$data['language'] = array();
			$data['strLanguage'] = "";
			
			if(is_array($data['agentLanguage']) && count($data['agentLanguage']) > 0) {
				foreach ($data['agentLanguage'] as $speciality) {
					array_push($data['language'], $speciality->Name);
				}
				
				$data['strLanguage'] = implode(', ', $data['language']);
			}
			
			//get listing which agent is in charging
			$data['agentListingMedia'] = array();
			$listings = $this->mdl_findanexpert->getListing($User_ID);
			if($listings) {
				foreach ($listings as $listing) {
					$img = $this->mdl_findanexpert->getAgentListingMedia($listing->Listing_ID, "Cover", 1);
					
					//$thumb = $this->imagemodel->resize($img->FileName . $img->FileExtension, 280, 200, HTTP_PROJECT_LISTING_IMAGE . $listing->Listing_ID . '/', $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . $listing->Listing_ID . '/');
					if(isset($img) && isset($img->FileName)&& isset($img->FileExtension)) {
						$thumb = $this->config->item('HTTP_LISTING_MEDIA_IMAGE') . $listing->Listing_ID . '/' . str_replace(' ', '%20', $img->FileName . '-280x200' . $img->FileExtension);
					} else {
						$thumb = $this->config->item('HTTP_IMAGE') . "tmp/no-image.png";
					}
					if(isset($listing->Price_Min) && $listing->Price_Min > 0) {
						$price = $listing->PriceUnit . " " . number_format(($listing->Price_Min), 0, '.', ',');
					} else {
						$price = "Price On Application";
					}
					
					if(isset($thumb)) {
						$data['agentListingMedia'][] = array(
							'listing_id'	=> $listing->Listing_ID,
							'thumb'			=> $thumb,
							'price'			=> $price,
							'Link'			=> $listing->Link,
						);
					}
				}
			}
			
			//get total of recommendations
			$data['total_recommendations'] = $this->mdl_findanexpert->getRecommendations($User_ID);
			//get content of recommendations
			$data['content_recommendations'] = $this->mdl_findanexpert->getRecommendationsInfo($User_ID);
			
			//get banner for right side
			$data['banner'] = $this->bannermodel->getBanners('Result', $this->config->item('market'), $this->config->item('config_language_id'));
			
			$data['module'] = "findanexpert";
			$data['view_file'] = "detail";

			echo Modules::run('template/default_template', $data);
		} else {
			redirect(base_url() . 'error_404', 'refresh');
		}
	}
	
	function getAgentProfilePhoto() {
		$assoc_id =$this->uri->segment(3,0);
		$bResult = true;
		
		$this->load->model('findanexpert/mdl_findanexpert');
		$result = $this->mdl_findanexpert->getagentphoto_($assoc_id);

		$imgStr = "";
		if($result) {
			if($result->Photo) {
				header('Content-Type: image/jpeg');
				echo $result->Photo;
			}
		}
	}
	
	function isPhoto($assoc_id) {
		$bResult = true;
		
		$this->load->model('findanexpert/mdl_findanexpert');
		$result = $this->mdl_findanexpert->getagentphoto_($assoc_id);

		if($result) {
			if(isset($result->Photo) && !empty($result->Photo)) {
				$bResult = true;
			} else {
				$bResult = false;
			}
		} else {
			$bResult = false;
		}
		
		return $bResult;
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */